package com.wenhuitec.sysui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

import com.wenhuitec.sysui.R;

/**
 * @Description 在添加菜品时底部库存弹出框
 */
public class ShowKuCunDialog {
    private View view;
    private EditText mStockEd;
    private EditText mMaxStockEd;
    private Button butSave;
    private Context mContext;

    public interface KuCunOnSave {
        void save(String stock, String maxStock, int autoStock);
    }

    public void BottomDialog(Context context, KuCunOnSave kuCunOnSave) {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(context, R.style.DialogTheme);
        //2、设置布局
        view = View.inflate(context, R.layout.dialog_kc, null);
        mMaxStockEd = view.findViewById(R.id.maxStockEd);
        mStockEd = view.findViewById(R.id.stockEd);
        butSave = view.findViewById(R.id.but_save);
        butSave.setOnClickListener(v -> {
            if (kuCunOnSave != null) {
                kuCunOnSave.save(mStockEd.getText().toString(), mMaxStockEd.getText().toString(), 0);
            }
            dialog.dismiss();
        });

        RadioButton noBuzuBut=view.findViewById(R.id.noBuzuBut);
        RadioButton autoBuzuBut=view.findViewById(R.id.autoBuzuBut);

//        setBounds(R.drawable.radiobutton,noBuzuBut);
//        setBounds(R.drawable.radiobutton,autoBuzuBut);
        dialog.setContentView(view);

        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.BOTTOM);
        //设置弹出动画
        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();
    }

    public void setDate(int stock, int maxStock) {
        mStockEd.setText(String.valueOf(stock));
        mMaxStockEd.setText(String.valueOf(maxStock));
    }

    /**
     * @param drawableId  drawableLeft  drawableTop drawableBottom 所用的选择器 通过R.drawable.xx 获得
     * @param radioButton 需要限定图片大小的radioButton
     */
    private void setBounds(int drawableId, RadioButton radioButton) {
        //定义底部标签图片大小和位置
        Drawable drawable_news = view.getResources().getDrawable(drawableId);
        //当这个图片被绘制时，给他绑定一个矩形 ltrb规定这个矩形  (这里的长和宽写死了 自己可以可以修改成 形参传入)
        drawable_news.setBounds(0, 0, 40, 40);
        //设置图片在文字的哪个方向
        radioButton.setCompoundDrawables(drawable_news, null, null, null);
    }

}